package com.iraeta.controller.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.common.Result;
import com.iraeta.common.ServiceException;
import com.iraeta.controller.BaseController;
import com.iraeta.entity.main.Role;
import com.iraeta.entity.main.TreeNode;
import com.iraeta.entity.main.User;
import com.iraeta.entity.main.vo.SysPermission;
import com.iraeta.service.sys.RoleService;
import com.iraeta.service.sys.UserService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController(value = "RoleController")
@RequestMapping("sys/role")
public class RoleController extends BaseController {
    @Resource
    RoleService roleService;
    @Resource
    UserService userService;

    @RequestMapping("pagerole")
    public String pageRole(@RequestBody JSONObject jo){
        String roleName = jo.getString("roleName");
        this.pageSet(jo);
        List<Role> roleList = roleService.getRoleByName(roleName);
        PageInfo<Role> rolePageInfo = this.getPageInfo(roleList);
        return JSON.toJSONString(Result.success(rolePageInfo));
    }

    @RequestMapping("getrole")
    public String getRole(@RequestBody JSONObject paramJb){
        int roleId = paramJb.getIntValue("id");
        Role role = roleService.getRole(roleId);
        return JSON.toJSONString(Result.success(role));
    }

    @RequestMapping("addeditrole")
    public String addRole(@RequestBody JSONObject paramJb){
        int roleId = paramJb.getIntValue("id");
        Role role = roleId == 0 ? new Role() :  roleService.getRole(roleId);
        this.mapJsonToObj(paramJb, role);
        if(roleId == 0){
            roleService.addRole(role);
        }else {
            roleService.updateRole(role);
        }
        return JSON.toJSONString(Result.success());
    }

    @RequestMapping("deleterole")
    public String deleteRole(@RequestBody JSONObject paramJb){
        int roleId = paramJb.getIntValue("id");
        roleService.deleteRole(roleId);
        return JSON.toJSONString(Result.success());
    }

    /*user role相关 根据角色选择用户 */
    @RequestMapping("listroleuser")
    public String listRoleUser(@RequestBody JSONObject paramJb){
        int roleId = paramJb.getIntValue("roleId");
        List<User> allUsers = userService.listUserByName("");
        List<User> roleUsers = userService.listUserByRoleId(roleId);
        JSONObject data = new JSONObject();
        data.put("allUsers", allUsers);
        data.put("roleUsers", roleUsers);
        return JSON.toJSONString(Result.success(data));
    }
    @RequestMapping("saveuserrole")
    public String saveUserRole(@RequestBody JSONObject paramJb){
        int roleId = paramJb.getIntValue("roleId");
        JSONArray roleUsersJA = paramJb.getJSONArray("roleUsers");
        List<Integer> roleUsers = new ArrayList<>();
        for(int i =0; i<roleUsersJA.size(); i++){
            roleUsers.add(roleUsersJA.getIntValue(i));
        }
        try {
            roleService.saveUserRole(roleId, roleUsers);
            return JSON.toJSONString(Result.success());
        }catch (Exception e){
            throw new ServiceException(e.getMessage());
        }
    }

    /*角色权限相关*/
    @RequestMapping("getrolepermission")
    public String getRolePermission(@RequestBody JSONObject paramJb){
        int roleId = paramJb.getIntValue("roleId");
        List<TreeNode> permissionAllNode = SysPermission.getPermissionTreeNode();
        List<Integer> rolePermissions = roleService.listRolePermission(roleId);
        List<String> checkedIds = rolePermissions.stream().map(Object::toString).collect(Collectors.toList());
        JSONObject data = new JSONObject();
        data.put("permissionAllNode",  permissionAllNode);	//获取权限的treeData数据
        data.put("checkedIds", checkedIds);                 //获取角色选择的权限数据
        return JSON.toJSONString(Result.success(data));
    }

    @RequestMapping("saverolepermission")
    public String saveRolePermission(@RequestBody JSONObject paramJb){
        int roleId = paramJb.getIntValue("roleId");
        JSONArray rolePermissionJA = paramJb.getJSONArray("rolePermissions");
        List<Integer> rolePermissions = new ArrayList<>();
        for(int i =0; i<rolePermissionJA.size(); i++){
            rolePermissions.add(rolePermissionJA.getIntValue(i));
        }
        try {
            roleService.saveRolePermission(roleId, rolePermissions);
            return JSON.toJSONString(Result.success());
        }catch (Exception e){
            throw new ServiceException(e.getMessage());
        }
    }
}
